import { Person } from "./person";
import { MyString } from "./my-string";
import { MyNumber } from "./my-number";
import { AbstractModel } from "../types/model-type";
/**
 * 模型枚举，
 * 包含所有模型
 */
export enum Model {
  Person,
  MyString,
  MyNumber
}
/**
 * 模型构建工厂
 */
export type FactoryRepository = {
  [model in Model]: (val: number) => AbstractModel;
};
/**
 * 模型构建工厂映射
 */
export const factoryRepository: FactoryRepository = {
  [Model.Person]: (val) => Person.common(val),
  [Model.MyString]: (val) => MyString.common(val),
  [Model.MyNumber]: (val) => MyNumber.common(val)
};
/**
 * 获取模型构建工厂，
 * 返回一个专属的模型构建工厂，
 * 带有缓存，
 * 传入number类型，如果相同则返回同一对象
 * @param model 模型类型
 */
export function modelFactory(model: Model): (val: number) => AbstractModel {
  const obj: { [num: number]: AbstractModel } = {};
  const fn = factoryRepository[model];
  return function (val: number): AbstractModel {
    if (!obj[val]) {
      obj[val] = fn(val);
    }
    return obj[val];
  };
}
